//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using NopSolutions.NopCommerce.BusinessLogic.Model;
using NopSolutions.NopCommerce.Common.Utils;
using NopSolutions.NopCommerce.Web.Modules;
using NopSolutions.NopCommerce.Controls;


namespace NopSolutions.NopCommerce.Web.Administration.Modules
{
    public partial class CategoryProductControl : BaseNopAdministrationUserControl
    {
        protected void Page_Load(object sender, EventArgs e) {
            btnAddNew.OnClientClick = string.Format("javascript:OpenWindow('CategoryProductAdd.aspx?cid={0}&BtnId={1}', 800, 600, true); return false;", this.CategoryId, btnRefresh.ClientID);

            if (!Page.IsPostBack) {
                this.BindData();
            }
        }

        private void BindData() {
            var category = this.CategoryService.GetCategoryById(this.CategoryId);

            if (category != null) {
                var existingProductCategoryCollection = category.ProductCategories;
                List<ProductCategoryMappingHelperClass> productCategoryMappings = GetProductCategoryMappings(existingProductCategoryCollection);
                gvProductCategoryMappings.Columns[1].Visible = this.SettingManager.GetSettingValueBoolean("Display.ShowAdminProductImages");
                if (productCategoryMappings != null && productCategoryMappings.Count > 0) {
                    gvProductCategoryMappings.Visible = true;
                    gvProductCategoryMappings.DataSource = productCategoryMappings;
                    gvProductCategoryMappings.DataBind();
                } else {
                    gvProductCategoryMappings.Visible = false;
                }
            } else {
                gvProductCategoryMappings.Visible = false;
            }
        }

        public void SaveInfo() {
            var category = this.CategoryService.GetCategoryById(this.CategoryId);

            if (category != null) {
                foreach (GridViewRow row in gvProductCategoryMappings.Rows) {
                    CheckBox cbProductInfo = row.FindControl("cbProductInfo") as CheckBox;
                    HiddenField hfProductId = row.FindControl("hfProductId") as HiddenField;
                    HiddenField hfProductCategoryId = row.FindControl("hfProductCategoryId") as HiddenField;
                    CheckBox cbFeatured = row.FindControl("cbFeatured") as CheckBox;
                    NumericTextBox txtRowDisplayOrder = row.FindControl("txtDisplayOrder") as NumericTextBox;

                    int productId = int.Parse(hfProductId.Value);
                    int productCategoryId = int.Parse(hfProductCategoryId.Value);
                    bool featured = cbFeatured.Checked;
                    int displayOrder = txtRowDisplayOrder.Value;

                    if (productCategoryId > 0 && !cbProductInfo.Checked)
                        this.CategoryService.DeleteProductCategory(productCategoryId);
                    if (productCategoryId > 0 && cbProductInfo.Checked) {
                        var productCategory = this.CategoryService.GetProductCategoryById(productCategoryId);
                        if (productCategory != null) {
                            productCategory.ProductId = productId;
                            productCategory.CategoryId = category.CategoryId;
                            productCategory.IsFeaturedProduct = featured;
                            productCategory.DisplayOrder = displayOrder;
                            this.CategoryService.UpdateProductCategory(productCategory);
                        }
                    }
                }
            }
        }

        protected void gvProductCategoryMappings_PageIndexChanging(object sender, GridViewPageEventArgs e) {
            gvProductCategoryMappings.PageIndex = e.NewPageIndex;
            BindData();
        }

        protected void btnRefresh_Click(object sender, EventArgs e) {
            this.BindData();
        }

        private List<ProductCategoryMappingHelperClass> GetProductCategoryMappings(List<ProductCategory> existingProductCategoryCollection) {
            List<ProductCategoryMappingHelperClass> result = new List<ProductCategoryMappingHelperClass>();
            foreach (ProductCategory pc in existingProductCategoryCollection) {
                Product product = pc.Product;
                if (product != null) {
                    ProductCategoryMappingHelperClass pcmhc = new ProductCategoryMappingHelperClass();
                    pcmhc.ProductCategoryId = pc.ProductCategoryId;
                    pcmhc.ProductId = pc.ProductId;
                    if (this.SettingManager.GetSettingValueBoolean("Display.ShowAdminProductImages")) {
                        pcmhc.ProductImage = GetProductImageUrl(product);
                    }
                    pcmhc.ProductInfo = product.Name;
                    pcmhc.IsMapped = true;
                    pcmhc.IsFeatured = pc.IsFeaturedProduct;
                    pcmhc.DisplayOrder = pc.DisplayOrder;
                    result.Add(pcmhc);
                }
            }

            return result;
        }

        public string GetProductImageUrl(Product product) {
            var picture = product.DefaultPicture;
            if (picture != null) {
                return this.PictureService.GetPictureUrl(picture, this.SettingManager.GetSettingValueInteger("Media.ShoppingCart.ThumbnailImageSize", 80));
            } else {
                return this.PictureService.GetDefaultPictureUrl(this.SettingManager.GetSettingValueInteger("Media.ShoppingCart.ThumbnailImageSize", 80));
            }
        }

        [Serializable]
        private class ProductCategoryMappingHelperClass
        {
            public int ProductCategoryId { get; set; }
            public int ProductId { get; set; }
            public string ProductImage { get; set; }
            public string ProductInfo { get; set; }
            public bool IsMapped { get; set; }
            public bool IsFeatured { get; set; }
            public int DisplayOrder { get; set; }
        }

        public int CategoryId {
            get {
                return CommonHelper.QueryStringInt("CategoryId");
            }
        }
    }
}